﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using TiTi.tea.Comm;
using TiTi.tea.Model;

namespace TiTi.tea.DAL
{
    public class BaseDAL<T> where T : class,new()
    {
        public readonly DbContext _dbContext = DbContextFactory.Create();

        /// <summary>
        /// 获取全部
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> GetAll()
        {
            return _dbContext.Set<T>();
        }

        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="predicate">查询条件</param>
        /// <returns></returns>
        public IQueryable<T> GetAll(Expression<Func<T, bool>> predicate)
        {
            return GetAll().Where(predicate);
        }

        /// <summary>
        /// 条件查询(排序)
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="predicate">查询条件</param>
        /// <param name="keySelector">排序依据</param>
        /// <param name="asc">是否升序</param>
        /// <returns></returns>
        public IQueryable<T> GetAll<TKey>(Expression<Func<T, bool>> predicate, Expression<Func<T, TKey>> keySelector,bool asc=false)
        {
            if (asc)
                return GetAll(predicate).OrderBy(keySelector);
            else
                return GetAll(predicate).OrderByDescending(keySelector);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="predicate">查询条件</param>
        /// <param name="keySelector">排序依据</param>
        /// <param name="pagePara">分页参数对象</param>
        /// <returns></returns>
        public IQueryable<T> GetAll<TKey>(Expression<Func<T, bool>> predicate, Expression<Func<T, TKey>> keySelector, PagePara pagePara)
        {
            return GetAll(predicate).SetPage(keySelector,pagePara);
        }

        /// <summary>
        /// 查询单个
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public T GetOne(Expression<Func<T, bool>> predicate)
        {
            return GetAll(predicate).FirstOrDefault();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity">要添加的实体</param>
        /// <param name="isSave">是否立即保存</param>
        /// <returns></returns>
        public bool Add(T entity,bool isSave = true)
        {
            _dbContext.Set<T>().Add(entity);
            if (isSave)
            {
                return SaveChanges();
            }
            return isSave;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">要添加的实体</param>
        /// <param name="isSave">是否立即保存</param>
        /// <returns></returns>
        public bool Update(T entity, bool isSave = true)
        {
            _dbContext.Entry(entity).State = EntityState.Modified;
            if (isSave)
            {
                return SaveChanges();
            }
            return isSave;
        }

        /// <summary>
        /// 伪删除
        /// </summary>
        /// <param name="entity">要添加的实体</param>
        /// <param name="isSave">是否立即保存</param>
        /// <returns></returns>
        //public bool Delete(long id, bool isSave = true)
        //{
        //    _dbContext.Entry(new T()).State = EntityState.Modified;
        //    if (isSave)
        //    {
        //        return SaveChanges();
        //    }
        //    return isSave;
        //}


        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        public bool SaveChanges()
        {
            return _dbContext.SaveChanges() > 0;
        }

    }
}
